import axios from 'axios'
import { stringify } from 'querystring'
const UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'

export const sleep = (ms: number) => {
    return new Promise(resolve => setTimeout(resolve, ms))
}

export const makeRequest = async (url: string, cookie: string): Promise<any> => {
    const headers = {
        'Cookie': cookie,
        'User-Agent': UA
    }
    const resp = await axios.get(url, { headers })
    return resp.data
}

export const jobListUrl = (page: number, city: string, position: string): string =>  {
    const baseUrl: string = 'https://www.zhipin.com/wapi/zpgeek/search/joblist.json'
    const searchParams: { [key: string]: string } = {
        scene: '1',
        query: '',
        city: city,
        experience: '',
        payType: '',
        partTime: '',
        degree: '',
        industry: '',
        scale: '',
        stage: '',
        position: position,
        jobType: '',
        salary: '',
        multiBusinessDistrict: '',
        multiSubway: '',
        page: page.toString(),
        pageSize: '30'
    }
    const encodedParams: string = stringify(searchParams)
    return `${baseUrl}?${encodedParams}`
}
